

<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>基于 RT-Thread 软件包开发指南 &mdash; CherryUSB 1.4.2 documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=e59714d7" />

  
      <script src="../_static/jquery.js?v=5d32c60e"></script>
      <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
      <script src="../_static/documentation_options.js?v=02f2166e"></script>
      <script src="../_static/doctools.js?v=9a2dae69"></script>
      <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="基于 ESP-Registry 开发指南" href="esp.html" />
    <link rel="prev" title="芯片通用移植指南" href="transplant.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            CherryUSB
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">快速上手</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="start.html">入门必看</a></li>
<li class="toctree-l1"><a class="reference internal" href="demo.html">基于现有 demo 快速验证</a></li>
<li class="toctree-l1"><a class="reference internal" href="transplant.html">芯片通用移植指南</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">基于 RT-Thread 软件包开发指南</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#id1">从机配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="#id2">主机配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="#stm32cubemx-usb">借助 STM32CubeMX 生成 USB 初始化</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="esp.html">基于 ESP-Registry 开发指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../q%26a.html">Q &amp; A</a></li>
<li class="toctree-l1"><a class="reference internal" href="../opensource.html">官方开源项目分享</a></li>
<li class="toctree-l1"><a class="reference internal" href="../share.html">开发者经验/开源项目分享</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">USB 基本知识点</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb2.0_basic.html">USB 基本概念(2.0 为主)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb3.0_basic.html">USB 基本概念(3.0 为主)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb_desc.html">USB 描述符</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb_request.html">USB 设备请求</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb_enum.html">USB 枚举</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb_ext.html">USB 知识点拓展</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">API 手册</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../api/api_device.html">设备协议栈</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/api_host.html">主机协议栈</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/api_port.html">主从驱动</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/api_config.html">USB CONFIG 说明</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Class 指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../class/class_cdc.html">CDC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/class_hid.html">HID</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/class_msc.html">MSC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/class_audio.html">UAC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/class_video.html">UVC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/winusb.html">WINUSB</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">例程说明</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_cdc_acm.html">usbd_cdc_acm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_hid.html">usbd_hid</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_msc.html">usbd_msc</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_rndis.html">usbd_rndis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_ecm.html">usbd_cdc_ecm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_audiov1.html">usbd_audiov1</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_audiov2.html">usbd_audiov2</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_video.html">usbd_video</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_winusb.html">usbd_winusb</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_webusb.html">usbd_webusb</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbh_serial.html">usbh_serial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbh_hid.html">usbh_hid</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbh_msc.html">usbh_msc</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbh_net.html">usbh_net</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbh_bluetooth.html">usbh_bluetooth</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbh_wifi.html">usbh_wifi</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbd_vendor.html">vendor device 驱动编写</a></li>
<li class="toctree-l1"><a class="reference internal" href="../demo/usbh_vendor.html">vendor host 驱动编写</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">USBIP 介绍</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../usbip/ohci.html">OHCI</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/ehci.html">EHCI</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/xhci.html">XHCI</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/chipidea.html">CHIPIDEA</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/dwc2.html">DWC2</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/musb.html">MUSB</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/fotg210.html">FOTG210</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/cdns2.html">CDNS2</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/cdns3.html">CDNS3</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/dwc3.html">DWC3</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">工具使用</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html">chryusb_configurator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html#usb-protocol-suite">力科 USB Protocol Suite</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html#wireshark">Wireshark</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html#audacity">Audacity</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">版本说明</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../version.html">版本说明</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">商业支持</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../support/index.html">商业支持</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">CherryUSB</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
      <li class="breadcrumb-item active">基于 RT-Thread 软件包开发指南</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../_sources/quick_start/rtthread.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="rt-thread">
<h1>基于 RT-Thread 软件包开发指南<a class="headerlink" href="#rt-thread" title="Link to this heading"></a></h1>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>CherryUSB 已经加入 RT-Thread 主线，可以选择使用主线版本，配置方式相同。</p>
</div>
<p>本节主要介绍使用 RT-Thread 提供的软件包管理器来配置工程，以 env 作为演示。本节操作不同芯片都一样，后续不再重复讲解。打开 env 以后使用 menuconfig 进入包管理器，并在如图所示路径中选择 CherryUSB。</p>
<figure class="align-default">
<img alt="../_images/env0.png" src="../_images/env0.png" />
</figure>
<section id="id1">
<h2>从机配置<a class="headerlink" href="#id1" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>选择 Enable usb device mode 并敲回车进入。</p></li>
<li><p>首先第一个配置是配置 USB 的速度，分为 <strong>FS、HS</strong>，表示使用全速还是高速功能。高速功能要求内置高速 PHY 或者外接 PHY</p></li>
<li><p>其次第二个配置则是选择 USB device ip，不清楚自己芯片是哪个 ip 的可以参考 <strong>port</strong> 目录下对应的 readme。</p></li>
<li><p>选择你想使用的 class</p></li>
<li><p>选择是否使用 demo 模板</p></li>
</ul>
<figure class="align-default">
<img alt="../_images/env1.png" src="../_images/env1.png" />
</figure>
<ul class="simple">
<li><p>最后退出保存即可。</p></li>
<li><p>拷贝 <cite>cherryusb_config_template.h</cite> 文件到自己工程目录下，命名为 <cite>usb_config.h</cite>，并添加相应的目录头文件路径,并修改以下内容：</p></li>
</ul>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;rtthread.h&quot;</span>

<span class="cp">#define CONFIG_USB_PRINTF(...) rt_kprintf(__VA_ARGS__)</span>
</pre></div>
</div>
<ul class="simple">
<li><p>USB IP 相关的 config 需要用户自己根据芯片实际情况修改</p></li>
<li><p>退出以后不急着编译，需要在代码中实现 <cite>usb_dc_low_level_init</cite> 函数。</p></li>
<li><p>调用 <cite>usbd_initialize</cite> 并填入 <cite>busid</cite> 和 USB IP 的 <cite>reg base</cite>， <cite>busid</cite> 从 0 开始，不能超过 <cite>CONFIG_USBDEV_MAX_BUS</cite></p></li>
<li><p>以上内容我们推荐放在 <strong>board.c</strong> 中，如下代码：</p></li>
</ul>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">OTG_HS_IRQHandler</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">USBD_IRQHandler</span><span class="p">(</span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">busid</span><span class="p">);</span>
<span class="n">USBD_IRQHandler</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">usbd_init</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">xxx_template_init</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">USB_OTG_HS_PERIPH_BASE</span><span class="p">);</span>
<span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">INIT_APP_EXPORT</span><span class="p">(</span><span class="n">usbd_init</span><span class="p">);</span>
</pre></div>
</div>
<ul class="simple">
<li><p>使用 <cite>scons –target=mdk5</cite> 或者 <cite>scons</cite> 进行编译，如果是mdk，需要使用 AC6 编译器</p></li>
</ul>
</section>
<section id="id2">
<h2>主机配置<a class="headerlink" href="#id2" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>选择 Enable usb host mode 并敲回车进入。</p></li>
<li><p>选择 USB host ip，不清楚自己芯片是哪个 ip 的可以参考 <strong>port</strong> 目录下对应的 readme。</p></li>
<li><p>根据需要勾选 class 驱动</p></li>
<li><p>选择是否开启模板 demo，请注意， msc 禁止使能，因为默认对接到 dfs。</p></li>
</ul>
<figure class="align-default">
<img alt="../_images/env2.png" src="../_images/env2.png" />
</figure>
<ul class="simple">
<li><p>最后退出保存即可。</p></li>
<li><p>拷贝 <cite>cherryusb_config_template.h</cite> 文件到自己工程目录下，命名为 <cite>usb_config.h</cite>，并添加相应的目录头文件路径,并实现以下内容：</p></li>
</ul>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;rtthread.h&quot;</span>

<span class="cp">#define CONFIG_USB_PRINTF(...) rt_kprintf(__VA_ARGS__)</span>
</pre></div>
</div>
<ul class="simple">
<li><p>USB IP 相关的 config 需要用户自己根据芯片实际情况修改</p></li>
<li><p>在代码中实现 <cite>usb_hc_low_level_init</cite> 函数</p></li>
<li><p>调用 <cite>usbh_initialize</cite> 并填入 <cite>busid</cite> 和 USB IP 的 <cite>reg base</cite>， <cite>busid</cite> 从 0 开始，不能超过 <cite>CONFIG_USBHOST_MAX_BUS</cite></p></li>
<li><p>以上内容我们推荐放在 <strong>board.c</strong> 中，如下代码：</p></li>
</ul>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">OTG_HS_IRQHandler</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">USBH_IRQHandler</span><span class="p">(</span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">busid</span><span class="p">);</span>
<span class="n">USBH_IRQHandler</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">usbh_init</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">usbh_initialize</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">USB_OTG_HS_PERIPH_BASE</span><span class="p">);</span>
<span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">INIT_APP_EXPORT</span><span class="p">(</span><span class="n">usbh_init</span><span class="p">);</span>
</pre></div>
</div>
<ul class="simple">
<li><p>使用 <cite>scons –target=mdk5</cite> 或者 <cite>scons</cite> 进行编译，如果是mdk，需要使用 AC6 编译器</p></li>
<li><p>如果使用的是 GCC ，需要在链接脚本(ld)中添加如下代码：</p></li>
</ul>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="cm">/* section information for usbh class */</span>
<span class="p">.</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ALIGN</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span>
<span class="n">__usbh_class_info_start__</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">.;</span>
<span class="n">KEEP</span><span class="p">(</span><span class="o">*</span><span class="p">(.</span><span class="n">usbh_class_info</span><span class="p">))</span>
<span class="n">__usbh_class_info_end__</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">.;</span>
</pre></div>
</div>
</section>
<section id="stm32cubemx-usb">
<h2>借助 STM32CubeMX 生成 USB 初始化<a class="headerlink" href="#stm32cubemx-usb" title="Link to this heading"></a></h2>
<p>使用 STM32CubeMX 主要是用来生成 usb 时钟、引脚、中断的配置。我们需要点击如图所示文件，并配置好 USB 的时钟、中断，点击 <cite>Generate Code</cite>。</p>
<figure class="align-default">
<img alt="../_images/stm32cubemx0.png" src="../_images/stm32cubemx0.png" />
</figure>
<figure class="align-default">
<img alt="../_images/stm32cubemx1.png" src="../_images/stm32cubemx1.png" />
</figure>
<figure class="align-default">
<img alt="../_images/stm32cubemx2.png" src="../_images/stm32cubemx2.png" />
</figure>
<figure class="align-default">
<img alt="../_images/stm32cubemx_clk.png" src="../_images/stm32cubemx_clk.png" />
</figure>
<ul class="simple">
<li><p>将 <cite>main.c</cite> 中的 <cite>SystemClock_Config</cite> 替换掉 <cite>board.c</cite> 中的配置</p></li>
</ul>
<figure class="align-default">
<img alt="../_images/stm32_init2.png" src="../_images/stm32_init2.png" />
</figure>
<ul class="simple">
<li><p>将 <cite>stm32xxxx_hal_msp.c</cite> 中的 <cite>HAL_PCD_MspInit</cite> 或者是 <cite>HAL_HCD_MspInit</cite> 中的内容复制到 <cite>usb_dc_low_level_init</cite> 和 <cite>usb_hc_low_level_init</cite> 函数中，举例如下：</p></li>
</ul>
<figure class="align-default">
<img alt="../_images/stm32_init.png" src="../_images/stm32_init.png" />
</figure>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="transplant.html" class="btn btn-neutral float-left" title="芯片通用移植指南" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="esp.html" class="btn btn-neutral float-right" title="基于 ESP-Registry 开发指南" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2022 ~ 2024, sakumisu.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>